package vn.cuhanh.adapter;

import java.io.File;
import java.util.ArrayList;

import vn.cuhanh.R;
import vn.cuhanh.bean.Banner;
import vn.cuhanh.customview.SingleShotImageView;
import vn.cuhanh.utils.HelperFunc;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

public class ImagePagerAdapter extends PagerAdapter {

	Context ctx;
	ArrayList<Banner> topBannersUrl;

	public ImagePagerAdapter(Context act, ArrayList<Banner> topBannersUrl) {
		this.topBannersUrl = topBannersUrl;
		this.ctx = act;

	}

	public int getCount() {
		return topBannersUrl.size();
	}

	public Object instantiateItem(View collection, int position) {
		LayoutInflater inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.hometopbanner_items, null);
		final String bannerUrl = topBannersUrl.get(position).getUrl();
		final Banner bannerItem = topBannersUrl.get(position);
		final ImageView banner = (SingleShotImageView) v
				.findViewById(R.id.bannersTopImg);
		final TextView bannerName = (TextView) v
				.findViewById(R.id.bannersTopName);
		final TextView bannerDescription = (TextView) v
				.findViewById(R.id.bannersTopDescription);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		v.setLayoutParams(lp);
		v.setBackgroundColor(Color.parseColor("#cbcbcb"));
		banner.setScaleType(ScaleType.FIT_XY);
		banner.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("ImagePagerAdapter", "item click");
			}
		});

		String name = HelperFunc.truncateString(topBannersUrl.get(position)
				.getName().toUpperCase(), 30);
		if (name != null) {
			bannerName.setText(name);
		}

		String description = HelperFunc.truncateString(
				topBannersUrl.get(position).getDescription(), 30);
		if (description != null) {
			bannerDescription.setText(description);
		}

		if (banner != null) {

			UrlImageViewHelper.setUrlDrawable(banner, bannerUrl,
					R.drawable.banner_bg);
			 String storagePath = Environment.getExternalStorageDirectory().toString()
			                      + ctx.getResources().getString(R.string.imgCacheFolder);
			 String image = bannerUrl.replaceAll("[^a-zA-Z 0-9]+", "");
			 final String filePath = storagePath + image;
			 File file = new File(filePath);
			 if (file.exists()) {
			
			 	 Bitmap bitmap = EfficientAdapter.decodeFile(filePath);
				 if (bitmap != null) {
					 /*
					 * bannerWidth = bitmap.getWidth(); bannerHeight =
					 * bitmap.getHeight();
					 */
					 banner.setImageBitmap(bitmap);
				 }
			 } else {
				 Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						EfficientAdapter.loadImageFromWebOperations(bannerUrl, filePath, null);
						((Activity)ctx).runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								Bitmap bitmap = EfficientAdapter.decodeFile(filePath);
								if (bitmap != null) {
									/*
									* bannerWidth = bitmap.getWidth(); bannerHeight =
									* bitmap.getHeight();
									*/
									banner.setImageBitmap(bitmap);
								}
							}
						});
					}
				});
				thread.start();
			 }
			/*
			 * if (HelperFunc.isTablet(m_context)) { banner.setLayoutParams(new
			 * LinearLayout.LayoutParams( bannerWidth * 5, bannerHeight * 3)); }
			 * else if (EfficientAdapter.screenSize ==
			 * EfficientAdapter.NORMAL_SCREEN) { banner.setLayoutParams(new
			 * LinearLayout.LayoutParams( bannerWidth * 2, bannerHeight * 2)); }
			 */
		}

		((ViewPager) collection).addView(v, 0);
		return v;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}
}
